package com.wmjsoft.plugins.security.base

import com.wmjsoft.plugins.security.user.CurrentUser
import com.wmjsoft.plugins.security.user.UserDetails
import java.time.LocalDateTime
import java.util.*

object InitCreateUpdateUser {

    fun <T: Any> initCreateUser(t:T){
        if(ClassUtils.hasColumn(t,"createdUserId")){
            var cu = CurrentUser.getCurrentUser()
            if(cu?.id == null){
                cu = UserDetails()
                cu.id = 0
                cu.username = "unknown"
            }
            ClassUtils.setProperty(t, "deleteStatus",0)
            ClassUtils.setProperty(t, "createdUserId", cu.id)
            ClassUtils.setProperty(t, "createdUserName",  cu.username)
            ClassUtils.setProperty(t, "createdDateTime", LocalDateTime.now())
        }
        initLastUpdateUser(t)
    }

    fun <T:Any> initLastUpdateUser(t:T){
        if(ClassUtils.hasColumn(t, "lastUpdateUserId")){
            var cu = CurrentUser.getCurrentUser()
            if(cu?.id == null){
                cu = UserDetails()
                cu.id = 0
                cu.username = "unknown"
            }
            ClassUtils.setProperty(t, "lastUpdateUserId",cu.id)
            ClassUtils.setProperty(t, "lastUpdateUserName",  cu.username)
            ClassUtils.setProperty(t, "lastUpdateDateTime", LocalDateTime.now())
        }
    }
}